var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [{
        name: '男',
        value: '男'
      },
      {
        name: '女',
        value: '女',
        checked: 'true'
      },
    ],
    items1: [{
        name: '父亲',
        value: '父亲',
        checked: 'true'
      },
      {
        name: '母亲',
        value: '母亲'
      },
      {
        name: '其他',
        value: '其他'
      },
    ],
    bloodTypeArray: ["A", "B", "AB", "O"],
    Typeindex: "0",
    date: '请选择出生日期',
    parent_name: "",
    parent_sfzid: "",
    username: "",
    gender: 2,
    shenggao: "",
    sfzid: "",
    mobile: "",
    stu_phone: "",
    remark: "",
    CharacterTrait: "",
    SpecialAttention: "",
    address: "",
    bloodType: "",
    relationship: "父亲",
    city: "重庆",
    otherUserid: "",
    formType:-1

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)
    console.log(options.form)
    this.setData({
      otherUserid:options.id,
      formType:options.form
    })
    // 参数  form 为 1 代表是从个人中心添加参营学院 中的添加按钮过来 2 代表是编辑 3 代表 是从 添加出行人那里过来
    var _this = this
    wx.getStorage({
      key: 'cityName',
      success: function (res) {
        console.log(res.data)
        _this.setData({
          city: res.data
        })
      }
    })
    var formType = options.form
    if (formType == "2") {
      _this.personalInformation(options.id)
    }
  },
  personalInformation: function (userid) {
    var that = this
    wx.request({
      url: 'https://miniapp.odeman.com/summercamp/new_travel_get.php',
      data: {
        userid: userid
      },
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        var data = res.data
        var list = data.list
        if (data.status == "success") {
          var items = []

          if (list.gender == 1) {
            items.push({
              name: '男',
              value: '男',
              checked: 'true'
            }, {
              name: '女',
              value: '女',

            })
          } else {
            items.push({
              name: '男',
              value: '男'
            }, {
              name: '女',
              value: '女',
              checked: 'true'
            })
          }
          var bloodType = that.data.bloodTypeArray

          for (var y in bloodType) {
            if (bloodType[y] == list.bloodType) {
              that.setData({
                Typeindex: y
              })
            }
          }
          var items1 = []
          if (list.relationship == "父亲") {
            items1 = [{
                name: '父亲',
                value: '父亲',
                checked: 'true'
              },
              {
                name: '母亲',
                value: '母亲'
              },
              {
                name: '其他',
                value: '其他'
              },
            ]
          } else if (list.relationship == "母亲") {
            items1 = [{
                name: '父亲',
                value: '父亲'

              },
              {
                name: '母亲',
                value: '母亲',
                checked: 'true'
              },
              {
                name: '其他',
                value: '其他'
              },
            ]
          } else {
            items1 = [{
                name: '父亲',
                value: '父亲'

              },
              {
                name: '母亲',
                value: '母亲',

              },
              {
                name: '其他',
                value: '其他',
                checked: 'true'
              },
            ]
          }
          var birthday = ""
          if (data.list.birthday == "") {
            birthday = "2012-10-20"
          } else {
            birthday = data.list.birthday
          }
          that.setData({
            date: birthday,
            username: data.list.username,
            parent_name: data.list.parent_name,
            parent_sfzid: data.list.parent_sfzid,
            sfzid: data.list.sfzid,
            mobile: data.list.mobile,
            stu_phone: data.list.stu_phone,
            relationship: data.list.relationship,
            address: data.list.address,
            CharacterTrait: data.list.CharacterTrait,
            SpecialAttention: data.list.SpecialAttention,
            remark: data.list.remark,
            items: items,
            shenggao: data.list.shenggao
          })
        }
      }
    })

  },
  radioSexChange: function (e) {
    console.log(e.detail.value)
    var sex;
    if (e.detail.value == "男") {
      sex = 1
    } else {
      sex = 2
    }
    this.setData({
      gender: sex
    })
  },
  user: function (e) {
    console.log(e.detail.value)
    this.setData({
      parent_name: e.detail.value
    })
  },
  idcard: function (e) {
    this.setData({
      parent_sfzid: e.detail.value
    })
  },
  babyName: function (e) {
    this.setData({
      username: e.detail.value
    })
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindBloodTypeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e)
    var index = e.detail.value
    var res = this.data.bloodType[index]
    console.log(res)
    this.setData({
      Typeindex: index,
      bloodType: res
    })
  },
  // 与学员的关系
  radioChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      relationship: e.detail.value
    })
  },
  // 地址
  address: function (e) {
    this.setData({
      address: e.detail.value
    })
  },
  // 备注
  remark: function (e) {
    this.setData({
      remark: e.detail.value
    })
  },
  // 性格
  character: function (e) {
    this.setData({
      CharacterTrait: e.detail.value
    })
  },
  // 关注
  attention: function (e) {
    this.setData({
      SpecialAttention: e.detail.value
    })
  },
  // 紧急联系人
  urgencyTel: function (e) {
    this.setData({
      stu_phone: e.detail.value
    })
  },
  // 家长电话
  patriarchTel: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  // 婴儿 ID
  babyIdCard: function (e) {
    this.setData({
      sfzid: e.detail.value
    })
  },
  //身高
  height: function (e) {
    this.setData({
      shenggao: e.detail.value
    })
  },
  // 保存
  save: function (e) {
    var that = this
    var idCardRegex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    var telRegex = /^1[3|4|5|6|7|8|9]\d{9}$/
    if (that.data.parent_name == "") {
      wx.showModal({
        title: '',
        content: '请输入监护人姓名'
      })
    } else if (!idCardRegex.test(that.data.parent_sfzid)) {
      wx.showModal({
        title: '',
        content: '请输入正确的身份证号'
      })
    } else if (that.data.username == "") {
      wx.showModal({
        title: '',
        content: '请输入婴儿姓名'
      })
    } else if (!telRegex.test(that.data.mobile)) {
      wx.showModal({
        title: '',
        content: '请输入家长联系电话'
      })
    } else if (!telRegex.test(that.data.stu_phone)) {
      wx.showModal({
        title: '',
        content: '请输入紧急联系电话'
      })
    } else if (that.data.address == "") {
      wx.showModal({
        title: '',
        content: '请填写地址'
      })
    } else {
      wx.showLoading({
        title: '保存中',
      })
      var url=""
      var userid = ""
      if (that.data.formType ==2) {
        url="https://miniapp.odeman.com/summercamp/new_travel_edit.php"
        userid=that.data.otherUserid
      } else {
        url='https://miniapp.odeman.com/summercamp/new_travel.php'
        userid=wx.getStorageSync('userid')
      }
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: url,
        data: {
          userid: userid,
          parent_name: that.data.parent_name,
          parent_sfzid: that.data.parent_sfzid,
          username: that.data.username,
          gender: that.data. gender,
          birthday: that.data.date,
          shenggao: that.data.shenggao,
          bloodType: that.data.bloodType,
          sfzid: that.data.sfzid,
          mobile: that.data.mobile,
          stu_phone: that.data.stu_phone,
          relationship: that.data.relationship,
          address: that.data.address,
          CharacterTrait: that.data.CharacterTrait,
          SpecialAttention: that.data.SpecialAttention,
          remark: that.data.remark

        },
        method: "post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success(res) {
          
          console.log(res.data)
          wx.hideLoading()
          if (res.data.status == "success") {
            wx.showToast({
              title: '添加成功',
              icon: 'success',
              duration: 3000
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            })
          }
        }
      })
    }
  }
})